//
//  AY_BookReadTimePage.m
//  AiYue
//
//  Created by boWen on 2023/11/14.
//

#import "AY_BookReadTimePage.h"
#import "AY_BookReadTimeCell.h"
@interface AY_BookReadTimePage ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UITableView * baseTableView;
@property (nonatomic, strong) NSArray * dataAry;
@end

@implementation AY_BookReadTimePage

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self initWithNav];
    [self getConfigData];
}

- (void)initWithNav {
    self.gk_navTitle = @"阅读记录";
    [self.view addSubview:self.baseTableView];
}

- (void)getConfigData {
    [AYApiRequest getUserReadTimeRecordSuccess:^(AY_ReadHistoryModel * _Nonnull pkg) {
        if([pkg.code isEqualToString:@"200"]) {
            self.dataAry = pkg.data;
            [self.baseTableView reloadData];
        }
    } failure:^(int status, NSString * _Nonnull info) {
        ShowToast(@"亲，网络不顺畅哟~");
    }];
}


- (UITableView *)baseTableView {
    if (!_baseTableView) {
        UITableView * myTableView = [AYTool creatMyTableView:CGRectMake(0, GK_STATUSBAR_NAVBAR_HEIGHT, GK_SCREEN_WIDTH, GK_SCREEN_HEIGHT - GK_STATUSBAR_NAVBAR_HEIGHT - GK_SAFEAREA_BTM)];
        myTableView.delegate = self;
        myTableView.dataSource = self;
        myTableView.backgroundColor = [UIColor clearColor];
        [myTableView registerClass:[AY_BookReadTimeCell class] forCellReuseIdentifier:@"AY_BookReadTimeCell"];
        _baseTableView = myTableView;
    }
    return _baseTableView;
}

- (NSArray *)dataAry {
    if(!_dataAry) {
        _dataAry = [NSArray array];
    }
    return _dataAry;
}


#pragma mark - UITableViewDelegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    AY_BookReadTimeCell * cell = [tableView dequeueReusableCellWithIdentifier:@"AY_BookReadTimeCell"];
    if (self.dataAry.count > indexPath.section) {
        cell.readTModel = self.dataAry[indexPath.section];
    }
    cell.addBookBlock = ^{
        [AYApiRequest addBookWithBookId:[self.dataAry[indexPath.section] articleId] andChapterNo:@"0" andPageNo:@"0" Success:^(AY_BaseModel * _Nonnull pkg) {
            ShowToast(@"添加书架成功");
            [self getConfigData];
        } failure:^(int status, NSString * _Nonnull info) {
            ShowToast(@"亲，网络不顺畅哟~");
        }];
    };
    return cell;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.dataAry.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return YNUM(92);
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [UIView new];
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return YNUM(10);
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    AY_PageReadViewController *vc = [[AY_PageReadViewController alloc] init];
    if (self.dataAry.count > indexPath.section) {
        [vc sendBookId:[self.dataAry[indexPath.section] articleId] andPage:@"0" andChapterIndex:[self.dataAry[indexPath.section] latestChapterNo]];
    }
    [self.navigationController pushViewController:vc animated:YES];
}

@end
